#include "stdafx.h"
#include "./glTools.h"

using namespace GLES;

int Memory::free_count = 0;

int Memory::alloc_count = 0;


Void* Memory::Alloc( size_t size )
{
	alloc_count ++;
	//return ::malloc(size);

	Void* p = ::new char[size];
	//xInfor2A("alloc %x size %d",p,size);
	return p;
}

Void Memory::Free( Void* p )
{

	free_count ++;
	//::free(p);
	//xInfor1A("freed %x",p);
	::delete p;
	return;
}

Void Memory::copy( Void* dst,const Void* src,size_t size )
{
	memcpy(dst,src,size);
	return;
}

Void*  Memory::operator new( unsigned int size )
{
	return Memory::Alloc(size);
}

Void*  Memory::operator new[]( unsigned int size )
{
	return Memory::Alloc(size);
}

Void* Memory::clone( size_t size,const Void* src )
{
	Void* p = Alloc(size);
	if( p == NULL)return NULL;
	copy(p,src,size);
	return p;
}

Void* Memory::Realloc( Void* p,size_t size )
{
#ifdef realloc
	Void* pnew = ::realloc(p,size);
	if( pnew == NULL)
	{
		free(p);
		return NULL;
	}
	return pnew;
#else
	//xAssert("not implemented");
	return NULL;
#endif
}

Void Memory::set( Void* dst,Uns32 n,size_t size )
{
	::memset(dst,n,size);
}

void  Memory::operator delete( void* p )
{
	return Memory::Free(p);
}

void  Memory::operator delete[]( void* p )
{
	return Memory::Free(p);
}